package ads_fld

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"
)

type AdsFldApi struct {
}

var adsFldService = AdsFldService{}

func (adsFldApi *AdsFldApi) CreateAdsFld(c *gin.Context) {
	var adsFld AdsFld
	err := c.ShouldBindJSON(&adsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsFld.CreatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"ApiId":      {utils.NotEmpty()},
		"SrcTbName":  {utils.NotEmpty()},
		"SrcFldName": {utils.NotEmpty()},
		"SrcFldType": {utils.NotEmpty()},
		"SrcFldLen":  {utils.NotEmpty()},
		"AdsFldName": {utils.NotEmpty()},
		"AdsFldType": {utils.NotEmpty()},
		"AdsFldLen":  {utils.NotEmpty()},
	}
	if err := utils.Verify(adsFld, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := adsFldService.CreateAdsFld(&adsFld); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func (adsFldApi *AdsFldApi) DeleteAdsFld(c *gin.Context) {
	var adsFld AdsFld
	err := c.ShouldBindJSON(&adsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsFld.DeletedBy = utils.GetUserID(c)
	if err := adsFldService.DeleteAdsFld(adsFld); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

func (adsFldApi *AdsFldApi) DeleteAdsFldByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := adsFldService.DeleteAdsFldByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

func (adsFldApi *AdsFldApi) UpdateAdsFld(c *gin.Context) {
	var adsFld AdsFld
	err := c.ShouldBindJSON(&adsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	adsFld.UpdatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"ApiId":      {utils.NotEmpty()},
		"SrcTbName":  {utils.NotEmpty()},
		"SrcFldName": {utils.NotEmpty()},
		"SrcFldType": {utils.NotEmpty()},
		"SrcFldLen":  {utils.NotEmpty()},
		"AdsFldName": {utils.NotEmpty()},
		"AdsFldType": {utils.NotEmpty()},
		"AdsFldLen":  {utils.NotEmpty()},
	}
	if err := utils.Verify(adsFld, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := adsFldService.UpdateAdsFld(adsFld); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

func (adsFldApi *AdsFldApi) FindAdsFld(c *gin.Context) {
	var adsFld AdsFld
	err := c.ShouldBindQuery(&adsFld)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if readsFld, err := adsFldService.GetAdsFldById(adsFld.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"readsFld": readsFld}, c)
	}
}

func (adsFldApi *AdsFldApi) GetAdsFldList(c *gin.Context) {
	var pageInfo AdsFldSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := adsFldService.GetAdsFldInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (adsFldApi *AdsFldApi) GetAllAdsFlds(c *gin.Context) {
	adsFld, err := adsFldService.GetAllAdsFlds()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsFld": adsFld}, "获取成功", c)
	}
}

func (adsFldApi *AdsFldApi) GetAdsFld(c *gin.Context) {
	adsFld, err := adsFldService.GetAdsFld()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"adsFld": adsFld}, "获取成功", c)
	}
}
